/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/gas-station
   @Language: C++
   @Datetime: 19-11-18 13:35
   */

class Solution {
public:
	int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
		int global=0, local=0, begin=0, n=gas.size();
		for(int i=0; i<n; ++i){
			global += gas[i]-cost[i];
			local += gas[i]-cost[i];
			if(local<0){
				begin = i+1;
				local = 0;
			}
		}
		return global<0 ? -1 : begin;
	}
};
